/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yard42.utils.algorithms;

/**
 *
 * @author Evgen
 */
public class QuickFind {

   private int[] id;

   public QuickFind(int N)
   {
      id = new int[N];
      for (int i = 0; i < N; i++)
      {
         id[i] = i;
      }
   }

   public boolean find(int p, int q)
   {
      return id[p] == id[q];
   }

   public void unite(int p, int q)
   {
      int pid = id[p];
      for (int i = 0; i < id.length; i++)
      {
         if (id[i] == pid)
         {
            id[i] = id[q];
         }
      }
      
      sout(p, q);
   }

   private void sout(int p, int q)
   {
      for (int i = 0; i < id.length; i++)
      {
         System.out.print(id[i] + " ");
      }

      System.out.println(p + "-" +q);
   }

   public int[] getId()
   {
      return id;
   }
}